package com.asset.utils;

import com.asset.model.entity.Asset;
import com.asset.model.entity.DepreciationDetail;
import com.asset.model.entity.DepreciationRecord;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;

/**
 * 折旧计算工具（直线折旧法）
 */
public class DepreciationUtil {

    private static final BigDecimal ONE = new BigDecimal("1");
    private static final BigDecimal MONTH = new BigDecimal("12");

    //返回折旧记录，实体类中包含了折旧明细的list
    public  static List<DepreciationDetail> calculationMethod(List<DepreciationDetail> depreciationDetails){
        //循环资产列表
        for(DepreciationDetail depreciationDetail : depreciationDetails){
            //获取这个资产的残值率
            BigDecimal residualRate = BigDecimal.valueOf(depreciationDetail.getAsset().getAssetCategory().getResidualRate());
            //获取使用年限
            BigDecimal usefulLife = BigDecimal.valueOf(depreciationDetail.getAsset().getAssetCategory().getUsefulLife());
            //计算月折旧率 (1-残值率) / (使用年限 * 12)
            BigDecimal monthDep = residualRate.subtract(ONE).divide(MONTH.multiply(usefulLife), 2, RoundingMode.HALF_UP);

            //折旧前净值
            BigDecimal netValueBefore = BigDecimal.valueOf(depreciationDetail.getNetValueBefore());

            //计算本期折旧金额(折旧前净值是按期提交从资产里获取，未按期提交从折旧详情表里获取)--折旧前净值*月折旧率
            BigDecimal currentDepreciation = monthDep.multiply(netValueBefore);

            //计算折旧后净值（折旧前净值-本期折旧金额）
            BigDecimal netValueAfter = netValueBefore.subtract(currentDepreciation);

            //计算累计折旧金额（累计折旧金额+本期折旧金额）
            BigDecimal cumulativeDepreciation = currentDepreciation.add(BigDecimal.valueOf(depreciationDetail.getCumulativeDepreciation()));

            //计算剩余折旧金额（折旧后净值-残值）
            BigDecimal remainingValue = netValueAfter.subtract(BigDecimal.valueOf(depreciationDetail.getAsset().getResidualValue()));

            //累计折旧期数
            depreciationDetail.setMonthsCumulative(depreciationDetail.getMonthsCumulative()+1);

            //剩余折旧期数
            depreciationDetail.setMonthsRemaining(depreciationDetail.getMonthsRemaining()-1);


            //先对列表的属性赋值
            depreciationDetail.setCurrentDepreciation(currentDepreciation.doubleValue());

            depreciationDetail.setNetValueAfter(netValueAfter.doubleValue());

            depreciationDetail.setCumulativeDepreciation(cumulativeDepreciation.doubleValue());

            depreciationDetail.setRemainingValue(remainingValue.doubleValue());

        }


        return depreciationDetails;
    }



    //更新单个资产的月折旧额
    public static Asset updateMonthlyDepreciation(Asset asset) {
        if (asset != null && asset.getAssetCategory() != null &&
                asset.getAssetCategory().getResidualRate() != null &&
                asset.getAssetCategory().getUsefulLife() >= 0) {
            //获取原值
            BigDecimal value = new BigDecimal(asset.getOriginalValue());
            //获取这个资产的残值率
            BigDecimal residualRate = BigDecimal.valueOf(asset.getAssetCategory().getResidualRate());
            //获取使用年限
            BigDecimal usefulLife = BigDecimal.valueOf(asset.getAssetCategory().getUsefulLife());
            //计算月折旧率 (1-残值率) / (使用年限 * 12)
            BigDecimal monthDep = ONE.subtract(residualRate).divide(MONTH.multiply(usefulLife), 2, RoundingMode.HALF_UP);

            //计算月折旧额(月折旧率 * 原值 )
            BigDecimal monthlyDepreciation = monthDep.multiply(value);

            //更新资产的月折旧额
            asset.setMonthlyDepreciation(monthlyDepreciation.doubleValue());
            return asset;
        } else {
            return null;
        }
    }
}
